﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Kinect;

namespace Final_Project
{
    class CFusionFileStrategy : CAbsFusionStrategy
    {
        private CDepthFileWriter m_cDepthFileWriter;
        private CDepthFileReader m_cDepthFileReader;
        private CKinectFusionAlg m_cKinectFusionAlg;
        private DepthImagePixel[] m_cDepthPixelDataRead;
        private short[] m_cDepthShortDataRead;
        private uint m_unNumberOfMaps = 0;
        private uint m_unNumberOfIterations = 0;
        private uint m_unFrameModulo = 1;

        public CFusionFileStrategy()
        {
            m_cDepthFileWriter = new CDepthFileWriter(CXmlConfigReader.Instance().ReadValueFromElement("FileNameAndPath"));
            m_cDepthFileReader = new CDepthFileReader(CXmlConfigReader.Instance().ReadValueFromElement("FileNameAndPath"));
            int nRows = Convert.ToInt32(CXmlConfigReader.Instance().ReadValueFromElement("DepthMapResolutionRows"));
            int nColumns = Convert.ToInt32(CXmlConfigReader.Instance().ReadValueFromElement("DepthMapResolutionColumns"));
            m_cDepthPixelDataRead = new DepthImagePixel[nColumns * nRows];
            m_cDepthShortDataRead = new short[nColumns * nRows];
            m_unFrameModulo = Convert.ToUInt32(CXmlConfigReader.Instance().ReadValueFromElement("FrameModulo"));

            // relocate alg creation
            m_cKinectFusionAlg = new CKinectFusionAlg();
        }

        public override void HandleNewDepthMap(DepthImagePixel[] values, short[] shortDepth)
        {
            if (m_unNumberOfIterations % m_unFrameModulo == 0)
            {
                m_cDepthFileWriter.WriteDepthMapToFile(values, m_unNumberOfMaps);
                m_cDepthFileWriter.WriteDepthShortsMapToFile(shortDepth, m_unNumberOfMaps);
                m_unNumberOfMaps++;
            }
            m_unNumberOfIterations++;
        }

        public override void RunAlg()
        {
            // **************** Test Code 
            
            m_unNumberOfMaps = 2;
            uint ind = 0; // just for testing
            
            for (uint i = 0; i < m_unNumberOfMaps; i++, ind += 7)
            {
                m_cDepthFileReader.ReadDepthMapFromFile(m_cDepthPixelDataRead, ind);
                m_cDepthFileReader.ReadDepthMapFromFile(m_cDepthShortDataRead, ind);
                m_cKinectFusionAlg.AddDepthMapToModel(ref m_cDepthPixelDataRead, ref m_cDepthShortDataRead);
            }
            
            // ****************
            /*
            for (uint i = 0; i < m_unNumberOfMaps; i++, m_unFrameModulo += 7)
            {
                m_cDepthFileReader.ReadDepthMapFromFile(m_cDepthPixelDataRead, m_unFrameModulo);
                m_cKinectFusionAlg.AddDepthMapToModel(m_cDepthPixelDataRead);
            }
            */
        }
    }
}
